#!/usr/bin/perl

# This script is wrote by Wladimir Palant with a minor edit by ChinaList
# and then another tiny edit by xiaody.

# Note: your subscription file should be saved in UTF-8 without BOM encoding, otherwise
# the generated checksum might be incorrect.

use strict;
use warnings;
use Digest::MD5 qw(md5_base64);

die "Usage: $^X $0 subscription.txt\n" unless @ARGV;

my $file = $ARGV[0];
my $data = readFile($file);

# Normalize data
$data =~ s/\r//g;

# Update timestamp
my $time = localtime(time);
$data =~ s/^!\s*Last modified:.*\n/!  Last modified: $time\n/gmi;

# Remove existing checksum
$data =~ s/^.*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//gmi;

my $data2 = $data;
$data2 =~ s/\n+/\n/g;

# Calculate new checksum
my $checksum = md5_base64($data2);

# Insert checksum into the file
$data =~ s/\n/\n!  Checksum: $checksum\n/;

writeFile($file, $data);

sub readFile
{
  my $file = shift;

  open(local *FILE, "<", $file) || die "Could not read file '$file'";
  binmode(FILE);
  local $/;
  my $result = <FILE>;
  close(FILE);

  return $result;
}

sub writeFile
{
  my ($file, $contents) = @_;

  open(local *FILE, ">", $file) || die "Could not write file '$file'";
  binmode(FILE);
  print FILE $contents;
  close(FILE);
}
